package com.authine.cloudpivot.common.config;

import com.authine.mvp.lib.mybatis.core.Mapper;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageHelper;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * @Author:tangjunjie
 * @Descriptioin:
 * @Date: 15:06 2020/9/23
 */
@Configuration
@Slf4j
public class MybatisPlusConfig {
    private static final String MAPPER_PACKAGE = ".mapper";
    private static final String MAPPER_INTERFACE_REFERENCE = Mapper.class.getCanonicalName();
    @Bean
    public PaginationInterceptor createPaginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDbType(DbType.MYSQL);
        paginationInterceptor.setDialect(new MySqlDialect());
        return paginationInterceptor;
    }

    public MybatisPlusConfig() {
    }

    public String getBasePackage(){
    	return "com.authine.cloudpivot.common.extension.customized";
    }

    @Bean
    public static DataSource dataSource(@Autowired DataSourceProperties dataSourceProperties) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(dataSourceProperties.getUrl());
        hikariConfig.setUsername(dataSourceProperties.getUsername());
        hikariConfig.setPassword(dataSourceProperties.getPassword());
        hikariConfig.setMaxLifetime(TimeUnit.MINUTES.toMillis(5L));
        hikariConfig.setConnectionTimeout(TimeUnit.MINUTES.toMillis(1L));
        hikariConfig.setValidationTimeout(TimeUnit.SECONDS.toMillis(30L));
        hikariConfig.setIdleTimeout(TimeUnit.SECONDS.toMillis(60L));
        log.info("初始化数据源成功：{}", dataSourceProperties.getUrl());
        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(@Autowired DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage(this.getBasePackage() + ".databaseobject");
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("pageSizeZero", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        pageHelper.setProperties(properties);
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        factory.setPlugins(new Interceptor[]{pageHelper,paginationInterceptor});
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
        log.info("装配mybatisPlus的SqlSessionFactory成功");
        return factory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        mapperScannerConfigurer.setBasePackage(this.getBasePackage() + MAPPER_PACKAGE);
        Properties properties = new Properties();
        properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE);
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        log.info("配置mybatis的扫描路径成功");
        return mapperScannerConfigurer;
    }


}
